import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import eslintPlugin from 'vite-plugin-eslint'
import { createHtmlPlugin } from 'vite-plugin-html'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'  //便于使用组件名
import { resolve } from 'path'
import autoprefixer from 'autoprefixer'

const name = 'vue3后台管理系统'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0',
    port: 9000,
    proxy: {
      // '^/api': {
      //   target: 'http://192.168.8.181:21029',
      //   changeOrigin: true
      // },
      // '^/permission': {
      //   target: 'http://192.168.8.181:44311',
      //   changeOrigin: true,
      //   rewrite: (path: string) => path.replace(/^\/permission/, '')
      // },
      '^/RGZ': { // 日照港线上地址
        target: 'http://e525e71303.zicp.vip',
        ws: true,
        changeOrigin: true
      },
      '^/wang': { // 王颖
        target: 'http://e525e71303.zicp.vip',
        ws: true,
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/wang/, '')
      },
      '^/cui': { // 崔哥
        target: 'http://192.168.8.118:21029',
        ws: true,
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/cui/, '')
      },
      '^/all': { // 全局接口
        target: 'http://192.168.8.181:14431',
        ws: true,
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/all/, '')
      },
      '^/oIdc': { // oidc-client登录
        target: 'http://192.168.8.181:14441',
        ws: true,
        changeOrigin: true,
        rewrite: (path: string) => path.replace(/^\/oIdc/, '')
      }
    }
  },
  css: {
    postcss: {
      plugins: [ autoprefixer({}) ]
    },
    preprocessorOptions: {
      scss: {
        additionalData: '@import "@/styles/variable.scss";'
      }
    }
  },
  plugins: [
    vue(),
    vueSetupExtend(),
    eslintPlugin({
      include: [ 'src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue' ]
    }),
    createHtmlPlugin({
      inject: {
        data: {
          title: name
        }
      }
    }),
    createSvgIconsPlugin({
      // 要缓存的图标文件夹
      iconDirs: [ path.resolve(__dirname, 'src/svg') ],
      // 执行 icon name 的格式
      symbolId: 'icon-[name]'
    })
    // legacy({
    //   targets: [ 'ie >= 11' ],
    //   additionalLegacyPolyfills: [ 'regenerator-runtime/runtime' ]
    // })
  ],
  build: {
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: false,
        drop_debugger: true
      }
    }
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src') // 路径别名
    },
    extensions: [ '.js', '.json', '.ts' ]
  }
})
